<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="110px"
    >
      <el-form-item label="提交人名称" prop="submit_user_name">
        <el-input
          v-model="queryParams.submit_name"
          placeholder="请输入提交人名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="客户名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入客户名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="客户手机号码" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入客户手机号码"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="客户地址" prop="address">
        <el-input
          v-model="queryParams.address"
          placeholder="请输入客户地址"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="是否报名" prop="registered">
        <el-select v-model="queryParams.registered" placeholder="请选择">
          <el-option
            v-for="item in selectOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['sale:enroll-clue:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list">
      <el-table-column label="提交人" align="center" prop="memberId" />
      <el-table-column label="客户名称" align="center" prop="clientName" />
      <el-table-column label="客户手机号码" align="center" prop="clientPhone" />
      <el-table-column label="客户地址" align="center" prop="clientAddress" />
      <el-table-column label="备注" align="center" prop="clientRemark" />
      <el-table-column label="学员账号" align="center" prop="studentEnrollClueId" />
      <el-table-column
        label="上报时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
      />
      <el-table-column label="有效天数" align="center" prop="validDays" />
      <!-- <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button
            link
            type="primary"
            v-hasPermi="['train:enroll-clue:update']"
            @click="openForm('update', scope.row.id)"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            v-hasPermi="['train:enroll-clue:delete']"
            @click="handleDelete(scope.row.id)"
          >
            删除
          </el-button>
        </template>
      </el-table-column> -->
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <EnrollClueForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts" name="EnrollClue">
import { dateFormatter } from '@/utils/formatTime'
import * as EnrollClueApi from '@/api/scrm/enroll-clue/index'
import EnrollClueForm from './enroll-clue-form.vue'
const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref([]) // 列表的数据
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  submit_name: null,
  name: null,
  phone: null,
  address: null,
  comment: null,
  registered: null,
  createTime: null
})
const selectOptions = ref([
  {
    label: '未报名',
    value: 0
  },
  {
    label: '已报名',
    value: 1
  }
])
const queryFormRef = ref() // 搜索的表单

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await EnrollClueApi.getPage(queryParams)
    // 数据处理
    data.list.forEach((item: any) => {
      item.registered = item.registered ? '已报名' : '未报名'
      item.exprTime = getClueExprTime(item.createTime, 31)
    })
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  console.log(queryParams)

  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: string) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: string) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await EnrollClueApi.del(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/**
 * 计算线索的剩余有效期
 */
const getClueExprTime = (dataTime: number, day: number) => {
  const now = new Date().getTime()
  const residueTime = Math.ceil((dataTime + day * 24 * 60 * 60 * 1000 - now) / 1000 / 60 / 60 / 24)
  if (residueTime > 0) {
    return residueTime + '天'
  } else {
    return '已过期'
  }
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
